<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Cashout extends CI_Controller {

    public function index($page = 0) {
    	
    	$sesMember = $this->session->userdata('sessionMember');
    	
    	if (!$sesMember) redirect(base_url() . 'memberlogin');
    	
    	$this->load->model("register_model");
        $check = $this->register_model->checkMember($sesMember);

        switch ($check['status']) {
            case "0":
                redirect(base_url() . 'memberlogin');
                break;
            case "1":
                redirect(base_url() . 'member/profile');
                break;
            case "2":
            	$this->_list($page);
                break;
        }
    }
    
    private function _list($page) {
    	
    	$sesMember = $this->session->userdata('sessionMember');
    	
    	$this->load->model('tree_model');
    	
    	$data				= array();
        $data['member']		= $this->tree_model->getNode($sesMember);
    	$data['comission']	= $this->tree_model->countComission($data['member']);
    	
    	$data['member']['limit']	= 10;
    	$data['member']['offset']	= $page;

    	$data['list']		= $this->tree_model->cashoutHistory($data['member']);
    	$data['jml']		= $this->tree_model->countCashoutHistory($data['member']);
    	
    	$this->load->library('pagination');
    	
    	$config['base_url']		= site_url('cashout/index/');
		$config['total_rows']	= (int) $data['jml'];
		$config['per_page']		= $data['member']['limit'];
		$config['uri_segment']	= 3;
		
		$this->pagination->initialize ( $config );
		
		$data ['paging'] = $this->pagination->create_links ();
    	
    	$data ['template'] ['nav'] = "essential/member_header";
		$data ['template'] ['content'] = 'member/cashout';
		$data ['template'] ['nosidebar'] = TRUE;
		
		$this->load->view ( 'essential/template', $data );
    }

    public function request() {
    	
    	$sesMember = $this->session->userdata('sessionMember');
    	
    	if (!$sesMember) redirect(base_url() . 'memberlogin');
            
		$this->load->model('tree_model');
		
		$node = $this->tree_model->getNode($sesMember);
		
		if($node) {
			
			$this->tree_model->cashoutRequest($node);
			
		}
    	
		redirect(site_url('cashout/index'));
    }
}